package com.powernode.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.Category;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdTagReference;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.SearchProdFeign;
import com.powernode.model.Result;
import com.powernode.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchProdFeign searchProdFeign;

    @Override
    public Page<Prod> queryWxProdListByTagId(Long current, Long size, Long tagId) {
        // 远程调用：根据分组标签id分页查询商品与分组标签的关系
        Result<Page<ProdTagReference>> result = searchProdFeign.getProdTagReferencePageByTagId(current, size, tagId);
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
            throw new BusinessException("调用失败：根据分组标签id分页查询商品与分组标签的关系");
        }
        // 获取商品与分组标签的分页对象
        Page<ProdTagReference> prodTagReferencePage = result.getData();
        // 判断对象是否有值
        if (ObjectUtil.isNull(prodTagReferencePage)) {
            throw new BusinessException("商品与分组标签关系分页对象为空");
        }
        // 从商品与分组标签的分页对象中获取关系记录
        List<ProdTagReference> prodTagReferenceList = prodTagReferencePage.getRecords();
        if (CollectionUtils.isEmpty(prodTagReferenceList) || prodTagReferenceList.size() == 0) {
            throw new BusinessException("商品与分组标签关系为空");
        }
        // 从商品与分组标签的关系集合中获取商品id集合
        List<Long> prodIdList = prodTagReferenceList.stream().map(ProdTagReference::getProdId).collect(Collectors.toList());
        // 远程调用：根据商品id集合查询商品对象集合
        Result<List<Prod>> prodResult = searchProdFeign.getProdListByIds(prodIdList);
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(prodResult.getCode())) {
            throw new BusinessException("调用失败：根据商品id集合查询商品对象集合");
        }
        // 获取商品对象数据
        List<Prod> prodList = prodResult.getData();
        // 创建商品分页对象
        Page<Prod> prodPage = new Page<>(current,size);
        prodPage.setRecords(prodList);
        prodPage.setTotal(prodTagReferencePage.getTotal());
        prodPage.setPages(prodTagReferencePage.getPages()) ;
        return prodPage;
    }

    @Override
    public List<Prod> queryWxProdListByCategoryId(Long categoryId) {
        List<Long> allCategoryIdList = new ArrayList<>();
        allCategoryIdList.add(categoryId);
        // 远程调用：查询当前类目的子类目集合
        Result<List<Category>> result = searchProdFeign.getCategoryListByParentId(categoryId);
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
            throw new BusinessException("调用失败：查询当前类目的子类目集合");
        }
        List<Category> childCategoryList = result.getData();
        // 判断是否有值
        if (!CollectionUtils.isEmpty(childCategoryList) && childCategoryList.size() != 0) {
            List<Long> childCategoryIdList = childCategoryList.stream().map(Category::getCategoryId).collect(Collectors.toList());
            allCategoryIdList.addAll(childCategoryIdList);
        }
        // 远程调用：根据商品类目id集合查询商品对象集合
        Result<List<Prod>> prodResult = searchProdFeign.getProdListByCategoryIds(allCategoryIdList);
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(prodResult.getCode())) {
            throw new BusinessException("调用失败：根据商品类目id集合查询商品对象集合");
        }
        List<Prod> prodList = prodResult.getData();

        return prodList;
    }
}
